package com.boomsecret.juc.atomic;

import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicMarkableReferenceDemo {

    public static void main(String[] args) {
        AtomicMarkableReference<String> atomicMarkableReference = new AtomicMarkableReference<>("aa", false);
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        atomicMarkableReference.set("bb", true);
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        // 感觉基本没用
        boolean[] markHolder = {false};
        System.out.println(atomicMarkableReference.get(markHolder));

        System.out.println("-------------");
        System.out.println(atomicMarkableReference.attemptMark("bb", false));
        System.out.println(atomicMarkableReference.isMarked());
        System.out.println(atomicMarkableReference.attemptMark("aa", true));
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        System.out.println(atomicMarkableReference.compareAndSet("bb", "cc", false, true));
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        System.out.println(atomicMarkableReference.compareAndSet("cc", "aa", false, true));
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());

        System.out.println("-------------");
        System.out.println(atomicMarkableReference.compareAndSet("bb", "aa", true, true));
        System.out.println(atomicMarkableReference.getReference());
        System.out.println(atomicMarkableReference.isMarked());
    }
}
